package cc.rengu.igas.share.core.service.trans;

import cc.rengu.igas.share.common.enums.SignTypeEnum;
import cc.rengu.igas.share.core.model.MchntInfo;
import cc.rengu.igas.share.core.model.PlatMchntInfo;
import cc.rengu.igas.share.core.realize.MchntService;
import cc.rengu.igas.share.core.realize.impl.MchntServiceImpl;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wukefan
 * @version 1.0.0
 * @date 2020/10/28 14:30
 */
public class EpaySignRelationInfoSyncService extends RadpService {
    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        try {

            //TODO 新老平台签约关系信息同步
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("签约关系信息同步处理失败,异常信息:<{}>", byteArrayOutputStream.toString());
            return -4;
        }

        return 0;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }

    private Map<String, String> getUserAndSignCruxInfo(String instId, String mchntNo) throws Exception {
        //初始值进行赋值
        boolean isGrantCredit = false;
        String userInst = instId;
        String signInst = instId;
        String mchntBelongInst;
        String signType;

        MchntService mchntService = new MchntServiceImpl();
        MchntInfo mchntInfo = mchntService.getMchntInfo(instId, mchntNo);
        PlatMchntInfo platMchntInfo;
        if (null == mchntInfo) {
            platMchntInfo = mchntService.getPlatMchntInfo(instId, mchntNo);
            if (platMchntInfo == null) {
                throw new BizException(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespCode(), OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespDesc());
            }

            if (!StringUtil.isEmptyOrNull(platMchntInfo.getGrantCreditFlag()) && "1".equals(platMchntInfo.getGrantCreditFlag())) {
                rglog.info("商户:<{}>,为授信商户", mchntNo);
                isGrantCredit = true;
                userInst = mchntNo;
            }

            mchntBelongInst = platMchntInfo.getSignInstId();
            signType = platMchntInfo.getSignType();
            if (SignTypeEnum.PLAT_CLASS_SIGN.getSignTypeCode().equals(signType)) {
                signInst = platMchntInfo.getPlatMchntNo();
            }
        } else {
            if (!StringUtil.isEmptyOrNull(mchntInfo.getPlatMchntNo())) {
                platMchntInfo = mchntService.getPlatMchntInfo(instId, mchntInfo.getPlatMchntNo());
                if (platMchntInfo != null && !StringUtil.isEmptyOrNull(platMchntInfo.getGrantCreditFlag()) && "1".equals(platMchntInfo.getGrantCreditFlag())) {
                    rglog.info("商户:<{}>,为平台商户子商户且平台商户为授信商户", mchntNo);
                    isGrantCredit = true;
                    userInst = platMchntInfo.getPlatMchntNo();
                }
            }

            mchntBelongInst = mchntInfo.getSignInstId();
            signType = mchntInfo.getMchntSignType();
            if (SignTypeEnum.MCHNT_CLASS_SIGN.getSignTypeCode().equals(signType)) {
                signInst = mchntInfo.getMchntNo();
            } else if (SignTypeEnum.PLAT_CLASS_SIGN.getSignTypeCode().equals(signType)) {
                signInst = mchntInfo.getPlatMchntNo();
            }
        }
        Map<String, String> param = new HashMap<>();
        param.put("userInst", userInst);
        param.put("signInst", signInst);
        param.put("signType", signType);
        param.put("mchntBelongInst", mchntBelongInst);
        param.put("grantCreditFlag", isGrantCredit ? AppParamConstant.YES : AppParamConstant.NO);
        return param;
    }
}
